#include<stdio.h>
#include"game.h"

//初始化棋盘
void deblock(char block[WIDE][HIGH])
{
	int i, j;
	for (i = 0; i < HIGH; i++)
	{
		for (j = 0; j < WIDE; j++)
		{
			block[i][j] = ' ';
		}
	}
}

//打印棋盘
void prblock(char block[WIDE][HIGH])
{
	int i, j;
	printf("\n\n");
	for (i = 0; i < HIGH; i++)
	{
		for (j = 0; j < WIDE; j++)
		{
			printf(" %c ", block[i][j]);
			if (j < WIDE - 1) printf("|");
			else printf("\n");
		}
		if(i<HIGH-1)
		for (j = 0; j < WIDE; j++)
		{
			printf("---");
			if (j < WIDE - 1) printf("+");
			else printf("\n");
		}
	}
	printf("\n\n");
}

//落子器
int  putgame(char block[WIDE][HIGH],int player,int x,int y)
{
	x--; y--;
	if (x<0||x>2||y<0||y>2)
	{
		printf("输入错误,坐标越界\n");
		return -1;
	}
	if (block[x][y]!=' ')
	{
		printf("输入错误，该处已有棋子\n");
		return -1;
	}
	if (player == 1) block[x][y] = 'x';
	if (player == -1) block[x][y] = 'o';
	return 0;
}

//判断结束游戏
int overgame(char block[WIDE][HIGH])
{
	int i,j;
	//横或竖
	for (i = 0; i < WIDE; i++)
	{
		if (block[i][0] == ' ') ;
		else if (block[i][0] != block[i][1]) ;
		else if (block[i][1] != block[i][2]) ;
		else
		{
			if (block[i][0] == 'x') printf("####一号玩家胜利####\n\n\n");
			if (block[i][0] == 'o') printf("####二号玩家胜利####\n\n\n");
			return 0;
		}
	}
	for (i = 0; i < WIDE; i++)
	{
		if (block[0][i] == ' ');
		else if (block[0][i] != block[1][i]) ;
		else if (block[1][i] != block[2][i]) ;
		else
		{
			if (block[0][i] == 'x') printf("####一号玩家胜利####\n\n\n");
			if (block[0][i] == 'o') printf("####二号玩家胜利####\n\n\n");
			return 0;
		}
	}
	//左上右下斜线
	if (block[0][0] == ' ') ;
    else if (block[0][0] != block[1][1]) ;
	else if (block[1][1] != block[2][2]) ;
	else
	{
		if (block[1][1] == 'x') printf("####一号玩家胜利####\n\n\n");
		if (block[1][1] == 'o') printf("####二号玩家胜利####\n\n\n");
		return 0;
	}
	//右上左下斜线
	if (block[0][2] == ' ');
	else if (block[0][2] != block[1][1]) ;
	else if (block[1][1] != block[2][0]) ;
	else
	{
		if (block[1][1] == 'x') printf("####一号玩家胜利####\n\n\n");
		if (block[1][1] == 'o') printf("####二号玩家胜利####\n\n\n");
		return 0;
	}
	//全屏占满
	for (i = 0; i < HIGH ; i++)
	{
		for (j = 0; j < WIDE; j++)
		{
			if (block[i][j] == ' ') return 1;
		}
	}
	printf("=====  平局！  =====\n\n");
	return 0;
}